﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EasyBuildingCMS.BLL.CoreEngine;
using EasyBuildingCMS.BuildingBlocks;
using EasyBuildingCMS.BuildingBlocks.BuildingBlockPlaceHolder;
using System.Text;
using EasyBuildingCMS.Base;

namespace EasyBuildingCMS
{
    public partial class Default : EditablePage
    {
        protected EasyBuildingCMS.BLL.CoreEngine.Page currentPage;
        protected PageRepository pageRepository = new PageRepository();

        public string PageUrl
        {
            get
            {
                if (HttpContext.Current.Request.QueryString["virtualHandleName"] == null)
                {
                    return null;
                }
                else
                    return "~/page_"+HttpContext.Current.Request.QueryString["virtualHandleName"] + ".aspx";
            }
        }

        protected void Page_PreInit()
        {
           currentPage = pageRepository.GetPageByUrl(PageUrl);
           if (currentPage == null) {
               currentPage = pageRepository.GetHomePage();
           }

           //set master page
           this.MasterPageFile = currentPage.MasterPage.MasterPageFile;

           if (!IsPostBack)
           {
                //set page attributes
                RegisterPageAttributes();

                BLL.Globals.CurrentPage = currentPage.PageID.ToString();
           }

            
            if (!HasEditRight)
                this.Master.FindControl("system_ControlPanel").Visible =false;
            else
                this.Master.FindControl("system_ControlPanel").Visible = true;

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadPageContent(currentPage);
            }
            
                
        }

        public void LoadPageContent(int pageID) {

            this.LoadPageContent(pageRepository.GetPageByID(pageID));
        }

        public void LoadPageContent(EasyBuildingCMS.BLL.CoreEngine.Page currentPage)
        {
            
            this.currentPage = currentPage;
            BuildingBlockInstanceRepository buildingBlockInstanceRepository = new BuildingBlockInstanceRepository();
            List<BuildingBlockZone> contentHolderTypeList = this.currentPage.MasterPage.BuildingBlockZone.ToList<BuildingBlockZone>();

            foreach (BuildingBlockZone contentHolderType in contentHolderTypeList)
            {
                BuildingBlockPlaceHolder buidlingBlocksPlaceHolder = (BuildingBlockPlaceHolder)Master.FindControl(contentHolderType.BuildingBlockName);
                if (buidlingBlocksPlaceHolder == null) continue;
                buidlingBlocksPlaceHolder.ClearBuildingBlocks();

                List<BuildingBlockInstance> buildingBlockInstances = buildingBlockInstanceRepository.GetSortedBuildingBlockInstanceByHolderPlace(currentPage.PageID, contentHolderType.BuildingBlockName);
                foreach (BuildingBlockInstance buildingBlockInstance in buildingBlockInstances)
                {
                    if (!buildingBlockInstance.Active) continue;
                   
                    BaseBuildingBlock buildingBlock = (BaseBuildingBlock)this.LoadControl(buildingBlockInstance.BuildingBlock.BuildingBlockFileName);

                    buildingBlock.BuildingBlockInstanceID = buildingBlockInstance.BuildingBlockInstanceID;
                    buildingBlock.BuildingBlockInstanceTitle = buildingBlockInstance.BuildingBlockInstanceFriendlyName;
                    buidlingBlocksPlaceHolder.BuildingBlocks.Controls.Add(buildingBlock);
                   
                }
            }

            ((UpdatePanel)(Master.FindControl("system_MainContentUpdatePanel"))).Update();
        
        }

        private void RegisterPageAttributes() 
        {
            //add title info to the page
            ContentPlaceHolder titlePlaceHolder = (ContentPlaceHolder)Master.FindControl("system_title");
            Literal title = new Literal();
            title.Text = currentPage.PageTitle;
            titlePlaceHolder.Controls.Add(title);

            //add meta info to the page
            ContentPlaceHolder headPlaceHolder = (ContentPlaceHolder)Master.FindControl("system_head");
            
            //add meta description
            Literal metaDescription = new Literal();
            string description = "";
            if (BLL.Globals.Settings.MetaDescription != null)
            {
                if (!string.IsNullOrEmpty(BLL.Globals.Settings.MetaDescription.Trim()))
                {
                    description = "<meta name=\"description\" content=\"" + BLL.Globals.Settings.MetaDescription + "\" />";
                }
            }
            if (currentPage.PageMetaDescription != null)
            {
                if (!string.IsNullOrEmpty(currentPage.PageMetaDescription.Trim()))
                {
                    description = "<meta name=\"description\" content=\"" + currentPage.PageMetaDescription + "\" />";
                }
            }
            metaDescription.Text = description;
            headPlaceHolder.Controls.Add(metaDescription);

            //add meta keywords
            Literal metaKeywords = new Literal();
            string keywords = "";
            if (BLL.Globals.Settings.MetaKeywords != null)
            {
                if (!string.IsNullOrEmpty(BLL.Globals.Settings.MetaKeywords.Trim()))
                {
                    keywords = "<meta name=\"keywords\" content=\"" + BLL.Globals.Settings.MetaKeywords + "\" />";
                }
            }
            if (currentPage.PageMetaDescription!=null)
            {
                if (!string.IsNullOrEmpty(currentPage.PageMetaDescription.Trim()))
                {
                    keywords = "<meta name=\"keywords\" content=\"" + currentPage.PageMetaKeywords + "\" />";
                }
            }
            metaKeywords.Text = keywords;
            headPlaceHolder.Controls.Add(metaKeywords);

        }

       

    }
}